using System;
using System.Collections.Generic;
using System.Text;

namespace ClientTools.Client.Controls
{
    public sealed class GridViewHelper
    {
        public static void SetGridLayout(System.Windows.Forms.DataGridView grid )
        {
            if (grid != null)
	        {
                grid.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.Lavender;
                grid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
                grid.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
                grid.RowTemplate.Height = 20;
                grid.AllowUserToAddRows = false;
                grid.AllowUserToResizeRows = false;
                grid.AllowUserToResizeColumns = true;
                grid.AllowUserToOrderColumns = true;
	        }
        }

        public static void SetDisplayColums(System.Windows.Forms.DataGridView grid, String visibleColumns, bool autoSize)
        {
            if (grid != null)
        	{
                System.Windows.Forms.DataGridViewColumn dc;

                foreach (System.Windows.Forms.DataGridViewColumn var in grid.Columns )
	            {
		            var.Visible = false;
	            }

                int i = 0;
                
                foreach (string s in visibleColumns.Split(";".ToCharArray()))
	            {
		            if (grid.Columns[s] != null)
                    {
                        dc = grid.Columns[s];
                        dc.Visible = true;
                        dc.DisplayIndex = i;
                        i++;
                        if (autoSize)
                        {
                            grid.Columns[s].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
                        }
                    }

	            }
            
                for (int x = 0; x < grid.Columns.Count; x++)
			    {
			        if (grid.Columns[x].DisplayIndex == i-1)
                    {
                        grid.Columns[x].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
                        return;
                    }
			    }
            }
        }

    //Public Overloads Shared Sub SetDisplayColums(ByVal grid As System.Windows.Forms.DataGridView, ByVal VisibleColumns As String, ByVal FillColumn As String)
    //    If Not grid Is Nothing Then

    //        Dim dc As System.Windows.Forms.DataGridViewColumn

    //        For Each dc In grid.Columns
    //            dc.Visible = False
    //        Next

    //        Dim i As Integer
    //        i = 0
    //        With grid.Columns
    //            For Each s As String In VisibleColumns.Split(";".ToCharArray)
    //                If Not .Item(s) Is Nothing Then
    //                    dc = .Item(s)
    //                    dc.Visible = True
    //                    .RemoveAt(.IndexOf(dc))
    //                    .Insert(i, dc)
    //                    i += 1
    //                    dc.AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells
    //                End If
    //            Next
    //        End With
    //        grid.Columns(FillColumn).AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.Fill
    //    End If
    //End Sub
    }
}
